import $ from 'jquery';
import { payRequest } from "./apis";

const request = (url,data,success,fail,type,asyncBoolean)=>{
    $.ajax({
    		
        url: url,
        timeout : 10000, //超时时间设置，单位毫秒
        type: type||"GET",
        dataType:url==payRequest?"xml":'json',
        data: data||{},
        asyncBoolean:asyncBoolean||true,
        headers: {
            "Access-Control-Allow-Origin":"*",
            "Access-Control-Allow-Headers":"X-Requested-With"
        }
        // success:function(result){
        //     console.log('success',result);
        // }
    }).done(function(result) {
        success &&success(result);
        // console.log("success");
        // console.log(result);
    }).fail(function(result) {
        if(result.readyState  == 4){
            success &&success(result.responseText);
        }else{
            fail &&fail(result);
        }
        console.log("error");
    })
}
//获取URL参数
const GetQueryString = (name)=>{
    if(name.match(/\[|\]|\-/) !=null && name.match(/\[|\]|\-/).length >0)name=name.replace(/\[/g,'\\[').replace(/\]/g,'\\]').replace(/\-/g,'\\-');
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = decodeURI(window.location.search.substr(1)).match(reg);
    if(r!=null)return  decodeURI(r[2]); return null;
}
    
export {request,GetQueryString}